package com.neusoft.hifly.rpc.email.dao.fallback;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.neusoft.hifly.core.log.Log;
import com.neusoft.hifly.core.pojo.ApiCode;
import com.neusoft.hifly.core.pojo.ApiResponse;
import com.neusoft.hifly.core.utils.GlobalConstants;
import com.neusoft.hifly.rpc.email.dao.IEmailDao;
import com.neusoft.hifly.rpc.email.domain.MailDTO;
import feign.hystrix.FallbackFactory;

/**
 * Hystrix 熔断
 */
@Component
public class EmailFallback implements FallbackFactory<IEmailDao> {

	@Override
	public IEmailDao create(final Throwable throwable) {
		final String msg = throwable == null ? "" : throwable.getMessage();
		if (!StringUtils.isEmpty(msg)) {
			Log.error(msg);
		}
		return new IEmailDao() {

			@Override
			public <T> ApiResponse<T> sendMail(final MailDTO obj) {
				return new ApiResponse<>(ApiCode.ERROR.getCode(), GlobalConstants.FEIGN_ERROR);
			}

		};
	}

}